//
//  LZFPicView.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/7/2.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFPicView.h"
#import "LZFTopicItem.h"
#import "UIImageView+Download.h"
#import "LZFSeeBigPicController.h"


@interface LZFPicView ()
@property (weak, nonatomic) IBOutlet UIImageView *backImageView;
@property (weak, nonatomic) IBOutlet UIImageView *gitImageView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigBtn;


@end
@implementation LZFPicView

//点击查看大图片按钮
- (IBAction)clickSeeBigPicBtn:(id)sender {
    
    //modal出查看大图片的控制器
    LZFSeeBigPicController *seeBigController=[[LZFSeeBigPicController alloc]init];
    seeBigController.topicItem=_topicItem;
    [self.window.rootViewController presentModalViewController:seeBigController animated:YES];
}


-(void)awakeFromNib
{
    self.autoresizingMask=UIViewAutoresizingNone;
    self.backImageView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [self.backImageView addGestureRecognizer:tap];
    
}

-(void)tap
{
    [self clickSeeBigPicBtn:nil];
}

-(void)setTopicItem:(LZFTopicItem *)topicItem
{
      _topicItem=topicItem;
    
//    [self.backImageView lzf_setImageWithOriginalImageURL:topicItem.image1 thumbnailImageURL:topicItem.image0 placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    //图片
    //如果是长图片的话,那么要重新绘制比例,不然会很难看
    [self.backImageView lzf_setImageWithOriginalImageURL:topicItem.large_image thumbnailImageURL:topicItem.small_image placeholderImage:[UIImage imageNamed:@"defaultUserIcon"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if (image==nil) return ;
        
        if (!topicItem.isBigPicture) return;
     
        //是长图片
        CGFloat imageW=self.topicItem.middleFrame.size.width;
        CGFloat imageH=self.topicItem.middleFrame.size.height;
        
            UIGraphicsBeginImageContext(CGSizeMake(imageW,imageH));
            [image drawInRect:CGRectMake(0, 0, imageW, imageW*self.topicItem.height/self.topicItem.width)];
             self.backImageView.image= UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
      
    }];
    
    
    //gif图片的显示
    self.gitImageView.hidden=!topicItem.is_gif;
    
    //按钮的显示
    self.seeBigBtn.hidden=!topicItem.isBigPicture;

  
}

@end
